package com.wlj.threadpooldemo;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author youan
 * @Date 2020/5/26 11:07 上午
 */
public class TestMain {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool(new MyThreadFactory(false));
        Counter counter = new Counter();
        for (int i=0;i<1;i++){
            Future<Integer> submit = executorService.submit(new TestCallable(0,counter));
            System.out.println(counter.a.get());
            try {
                System.out.println(submit.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        executorService.shutdown();




    }

    static class Counter{
        AtomicInteger a=new AtomicInteger(0);
    }

    static class TestCallable implements Callable<Integer>{

        private int a;

        private Counter counter;

        public TestCallable(int a,Counter counter) {
            this.a = a;
            this.counter=counter;
        }

        @Override
        public Integer call() throws Exception {
            for (int i = 0; i < 5; i++) {
                a++;
                counter.a.getAndIncrement();
                Thread.sleep(1000);
            }
            return a;
        }
    }
}
